import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { WisdomGroupMoneyStrategy } from "./WisdomGroupMoneyStrategy";
import { NormalGroupMoneyStrategy } from "./NormalGroupMoneyStrategy";
import { WinRabbitMoneyStrategy } from "./WinRabbitMoneyStrategy";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";

export class WisdomMoneyStrategy implements IMoneyStrategy {
    subPlan: IPlan[];
    debugCompoenent: string = "WisdomMoneyStrategyDebug";
    strategyName: string;
    constructor(name?: string) {
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        throw new Error("Method not implemented.");
    }
    resetRecm(): void {
    }
    resetStrategy(): void {
        //正向，兔子算法
        let f_rabbit_plan = new ExcutePlan();
        f_rabbit_plan.planName = "正向兔子";
        f_rabbit_plan.recmStrategy = [new FixedStrategy({ reverse: false })]
        f_rabbit_plan.moneyStrategy = [new WinRabbitMoneyStrategy()];
        f_rabbit_plan.toResult(new RoundInfo());

        //反向，兔子算法
        let p_rabbit_plan = new ExcutePlan();
        p_rabbit_plan.planName = "反向兔子";
        p_rabbit_plan.recmStrategy = [new FixedStrategy({ reverse: true })]
        p_rabbit_plan.moneyStrategy = [new WinRabbitMoneyStrategy()]
        p_rabbit_plan.toResult(new RoundInfo());

        //根据自己记录胜负数投注，红色正向投注，蓝色反向投注，0不投，

        let normalGroupPlan = new ExcutePlan();
        normalGroupPlan.planName = "正常分组";
        normalGroupPlan.recmStrategy = [new FixedStrategy({ reverse: false })]
        normalGroupPlan.moneyStrategy = [new NormalGroupMoneyStrategy("")];
        normalGroupPlan.toResult(new RoundInfo());

        this.subPlan = [f_rabbit_plan, p_rabbit_plan, normalGroupPlan];
        for (let i = 0; i < 5; i++) {
            let gi = new ExcutePlan();
            gi.planName = `正向${i + 1}组`;
            gi.baseMoney = 200;
            gi.recmStrategy = [new FixedStrategy({ reverse: false })];
            gi.moneyStrategy = [new WisdomGroupMoneyStrategy(`$wisdom-group-${i}`, {
                groupCount: (i + 1),
                addStep: 200,
                devideStep: -200,
                maxLevel:3000,
                reverse: false, //true:赢了+100，输了-100
            })]
            gi.toResult(new RoundInfo());
            this.subPlan.push(gi);

            let gif = new ExcutePlan();
            gif.planName = `反向${i + 1}组`;
            gif.baseMoney = 200;
            gif.recmStrategy = [new FixedStrategy({ reverse: true })];
            gif.moneyStrategy = [new WisdomGroupMoneyStrategy(`$reverse-wisdom-group-${i}`, {
                groupCount: (i + 1),
                addStep: 200,
                devideStep: -200,
                maxLevel:3000,
                reverse: false, //true:赢了+100，输了-100
            })]
            gif.toResult(new RoundInfo());
            this.subPlan.push(gif);
        }

    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        this.subPlan.forEach((pl, index) => {
            pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);
            let userRecmV = pl.toResult(roundInfo);
            let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
            pl.userRecmInfo.recmV = userRecmV;

            let [rv, rm] = pl.toMoney(newRoundInfo, recmInfo, userRecm, pl.baseMoney);
            pl.recmInfo.preBetCount = pl.recmInfo.betCount;
            if (rv != EnumBjlResult.U) {
                //pl.recmInfo.
                pl.recmInfo.betCount++;
            }
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;

            if (index >= 3 && index % 2 == 0) {
                let prePlan = this.subPlan[index - 1];
                let winMoney = pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney;
                let prevMoney = prePlan.recmInfo.summaryInfo.winMoney - prePlan.recmInfo.summaryInfo.failMoney;
                if (((winMoney > 0 && prevMoney >=0 )|| (winMoney >=0 && prevMoney > 0))
                    && pl.recmInfo.betCount > 1
                ) {
                    (pl as ExcutePlan).resetPlan();
                    (prePlan as ExcutePlan).resetPlan();
                    // pl.moneyStrategy.forEach(item => (item as ExcutePlan).resetPlan());
                    // prePlan.moneyStrategy.forEach(item => (item as ExcutePlan).resetPlan());


                    if (index == 4) {
                        //this.subPlan[2].moneyStrategy.forEach(item => (item as ExcutePlan).resetPlan());
                        (this.subPlan[2] as ExcutePlan).resetPlan();
                    }
                }

            }

            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += pl.recmInfo.recmMoney;

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += pl.recmInfo.recmMoney;
            }
        })
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            1
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }

        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategy;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}